#
# CVSPrefs.py
#
# Preferences and Static Variables for SNiFF+ 3.x CVS Adapter
#
# Author: Martin Oberhuber
# Copyright (c) 1998, 1999 TakeFive Software, Salzburg
#
# Revisions:
#

from vcs import NL, TRUE, FALSE
import vcs
import string
import os

##############################################################################
# User configurable section begins
##############################################################################

TEST=0				# Debug output
TEST2=0				# HEAVY debug output including RepositoryFile()

LockersShowEntries=1	# Show current checked-out version in the lockers column
LockersShowExclusive=1	# Show exclusive lockers in the lockers column
LockersShowEditors=0	# Show concurrent lockers (editors) in the lockers column
LockersShowSticky=1		# Show sticky tags in the lockers column
AllowExclusiveLocking=1	# Menu Entries for exclusive locking are active
AllowMergeInSniffUpdate=1 # Allow updating private writable files with cvs update
DoCVSEdit=1				# Use cvs edit (0: just use local chmod to set writable)
FastModified=0			# Use Timestamp information instead of diff for IsDifferent() for HEAD
RelativeCVSReps=1		# Allow Relative CVS Repositories
AlwaysShowRepDialog=0	# Always show the Dialog for entering the remote Repository path
DoFastLog=0				# Try to do fast History by using rlog for "Local" Rep
ChangeRemoteRepUser=1	# Change username to local user for "user@server:" type Repository
AllowBranchOpsInCM=0	# Allow Creating / Deleting branches in CM: 
						# This seems to work only with CVS-1.9, not CVS-1.10
EditRemoveBaseForPDF=0	# Remove CVS/Base file for PDFs to avoid confusion in OpenDialog
RequireComment = 0		# Allow CheckIn only if a comment is given
RequireChangeset = 0	# Allow CheckIn only if a changeset is given

## how to manage concurrent and exclusive locks for "retain lock" at CheckIn()
gRetainExNever = 0		# Never make exclusive lock at "Retain Lock"
gRetainExAlways = 1		# Always retain with exclusive lock
gRetainExIfEx = 2		# Retain exclusive lock if it was exclusively locked before
gRetainExAsk = 3		# Ask the user whether to retain exclusive or concurrent
RetainLockMode=gRetainExIfEx # Take exclusive locks into account when "Retain lock"

# The AccessControl Lists. To Disable Access Control, leave the empty ACLs.
# By overwriting each of the ACLs with an empty list / dictionary, ACLs are disabled.
# To enable them, simply comment out the empty version and configure the other one
# to your needs.

# The AdminUsers list can be re-used in any of the following Access Control Lists.
AdminUsers = [ "sniffadm" ]

# The users in AdminACL are allowed to use the following features:
#  - "Delete Version"
#  - "Delete Symbol" in the CM
#  - "Rename Symbol" in the CM
#  - "Delete Branch name" in the CM if also AllowBranchOpsInCM is set on
#  - "Set Symbol for branch" in the CM if also AllowBranchOpsInCM is set on
#  - "Rename Branch" in the CM if also AllowBranchOpsInCM is set on
AdminACL = AdminUsers
AdminACL = []

# Only Users listed in the BreakLockACL are allowed to break locks.
BreakLockACL = AdminUsers
BreakLockACL = []
 
# The BranchACL is a Dictionalry with pairs of (branchName, AllowedUserList)
# the empty Branch is the main trunk
BranchACL = {
	""		:	AdminUsers + [ "integrator2", "integrator3" ],
	"PG1"	:	AdminUsers + [ "integrator2", "integrator3" ],
	"PG2"   :   AdminUsers + [ "integrator2", "integrator3" ]
}
BranchACL = {}

# For tags present in the tagACL, only the users listed in the ACL are allowed
# to set, move or delete this tag name.
TagACL = {
	"PG1"		:	[],
	"PG2"   	:   [],
	"FREEZE_1"	:	[ "admin1", "admin2", "admin3" ]
}
TagACL = {}

CVSTempDir="%s/.sniffdir2/CVS"	# Temp. Directory for EditAdminFile: %s is replaced with the OSTempDir

maxOptimize = 32000		# Maximum commandline length

##############################################################################
# More configuration constants for later expansion, not yet active
##############################################################################

ConflictsFile="%w/CVSConflicts.sniff"

DoModuleCo=0			# Allow cvs Modules Checkout: requires PDFs to be named same
						# as in the cvs modules file
FastUpdate=0			# Use fast cvs/update algorithm for SNiFFUpdate

gGuessFromProjectDir = 1	# try to find CVS info in the main Project directory
							# and construct local one according to catDir
gGuessRCCS = 2				# use the RCCSpath from the Project description file
							# and construct local RepositoryDir according to catDir
gGuessParent = 3			# use the local parent directory until something is found
gGuessProjectParent = 4		# use the Project Directory's parent
gGuessProjectDir = 5		# directly take the relative path

##############################################################################
# End of configurable section
##############################################################################

# The list of known CVSROOTs together with local translations.
# Local translations are needed for the cache (CheckRepositoryDate),
# and for breaking locks; 
# They could also be used for faster access in some environments.
#_TRANSLATIONS = { "@TEST" : { ":server:mober@apollo:/Users/mober/cvsroot" : ""  } }
_TRANSLATIONS = {}

def _ReadTranslationFile(RWE):
	"""Return a dictionary from TranslationFile if it is readable"""
	if not RWE: 
		return {}
	if RWE[0] != '@':
		return { RWE : "" }
	# Read from file
	try:
		transList = open(RWE[1:]).read()
	except:
		print "(CVSPrefs) _ReadTranslationFile: ERROR: Cannot read RWE file",RWE[1:]
		return {}
	result = {}
	if TEST2: print "(CVSPrefs) _ReadTranslationFile: Read ",transList
	for line in string.split(transList,NL):
		tmp = string.split(line)
		if not tmp: continue
		if len(tmp) == 2:
			result[tmp[0]] = tmp[1]
		elif len(tmp) == 1:
			result[tmp[0]] = ""
		else:
			print "(CVSPrefs) _ReadTranslationFile: WARNING: illegal entry ",tmp
	return result

def IsUniqueRepository(RWE):
	"""Return TRUE if there are no multiple Repositories in the RWE given"""
	if not RWE:
		return FALSE
	if RWE[0]=="@":
		return FALSE
	if len(GetRepositories(RWE)) != 1:
		return FALSE
	return TRUE

def AddTranslation(RWE,root,trans=""):
	"""Add a CVSROOT, overwriting existing same one only if new Translation"""
	if not _TRANSLATIONS.has_key(RWE):
		_TRANSLATIONS[RWE] = _ReadTranslationFile(RWE)
	if not trans:
		if _TRANSLATIONS[RWE].has_key(root):
			return
	_TRANSLATIONS[RWE][root]=trans

def GetRepositories(RWE):
	"""Get a list of all known CVSROOTs"""
	if not _TRANSLATIONS.has_key(RWE):
		_TRANSLATIONS[RWE] = _ReadTranslationFile(RWE)
	return _TRANSLATIONS[RWE].keys()

def GetTranslations(RWE):
	"""Get a list of all known CVSROOTs"""
	if not _TRANSLATIONS.has_key(RWE):
		_TRANSLATIONS[RWE] = _ReadTranslationFile(RWE)
	return _TRANSLATIONS[RWE]

def TranslateRoot(RWE,old_root):
	"""can translate a CVSROOT to a corresponding local root;
	maintains the list of CVSROOTs"""
	if not _TRANSLATIONS.has_key(RWE):
		_TRANSLATIONS[RWE] = _ReadTranslationFile(RWE)
	try:
		new_root = _TRANSLATIONS[RWE][old_root]
	except KeyError:
		# add an empty entry for a newly found CVSROOT
		_TRANSLATIONS[RWE][old_root] = ""
		return old_root
	if not new_root:
		return old_root
	else:
		return new_root
		
def GetTempDir(RWE=""):
	try:
		tmpDir=os.environ["TMPDIR"]
	except:
		if os.name=='posix':
			tmpDir="/tmp"
		else:
			try:
				tmpDir=os.environ["TEMP"]
			except:
				print "(CVSPrefs) GetTempDir: ERROR - cannot determine temp directory, trying C:"
				tmpDir = "C:"
	tmpBase = CVSTempDir % tmpDir
	if RWE:
		tr=string.maketrans(":/;@, ","______")
		tmpBase = vcs.joinpath(tmpBase, string.translate(RWE,tr))
	vcs.mkdir_p(tmpBase)
	return tmpBase

def GetUserName():
	try:
		userName=os.environ["USER"]
	except:
		try:
			userName=os.environ["USERNAME"]
		except:
			print "(CVSPrefs) GetUserName: ERROR - cannot determine current user name, trying ", vcs.user
			userName = vcs.user
	return userName

def LoadPrefs():
	model = PotGuiModel()
	model.SetValue("Test", 1)
	data=model.DumpModel()
	f=open("C:/test.txt",'w')
	f.write(data)
	f.close()

def SavePrefs():
	f=open("C:/test.txt",'r')
	data=f.read()
	f.close()
	model = PotGuiModel()
	model.UnDumpModel(data)

